/*
 * ChooseBankDueDateDialog.java
 * business
 *
 * Created by SunDing on 2017/2/22.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.datepicker;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 选择信用卡到期日期Dialog
 * <p>
 * Created by SunDing(dindding@163.com) on 2017/2/22 11:33
 */
public class ChooseBankDueDateDialog extends Dialog implements View.OnClickListener {

    private ImageView mIvClose;
    private TextView mTvFinish;
    private ScrollerNumberPicker mYear;
    private ScrollerNumberPicker mMonth;
    private OnChooseBankDueDateListener mListener;

    public ChooseBankDueDateDialog(Context context) {
        super(context, R.style.BottomDialogTheme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_bank_due_date);
        initViews();
        initWindow();
        initData();
        initListeners();
    }

    private void initListeners() {
        mIvClose.setOnClickListener(this);
        mTvFinish.setOnClickListener(this);
    }

    private void initWindow() {
        // 改变样式-->window的样式
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        // 设置弹出位置
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;

        window.setAttributes(params);
    }

    private void initViews() {

        mIvClose = (ImageView) findViewById(R.id.iv_close);
        mTvFinish = (TextView) findViewById(R.id.tv_finish);
        mYear = (ScrollerNumberPicker) findViewById(R.id.year);
        mMonth = (ScrollerNumberPicker) findViewById(R.id.month);
    }

    private void initData() {
        List<String> yearList = new ArrayList<>();
        List<String> monthList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        for (int i = 0; i < 100; i++) {
            yearList.add(year + i + "");
        }
        for (int i = 1; i < 13; i++) {
            monthList.add(i + "");
        }
        mYear.setData(yearList);
        mMonth.setData(monthList);
        mYear.setDefault(0);
        mMonth.setDefault(0);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_finish:
                dismiss();
                if (mListener != null) {
                    mListener.OnChooseBankDueDate(mYear.getSelectedText(),
                            mMonth.getSelectedText());
                }
                break;
        }
    }


    public void setOnChooseBankDueDateListener(OnChooseBankDueDateListener listener) {
        mListener = listener;
    }

    public interface OnChooseBankDueDateListener {

        void OnChooseBankDueDate(String year, String month);
    }


}
